package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.LostAndFound.dao.MyIssueDao;
import com.LostAndFound.domain.Article;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.utils.DataSourceUtils;

public class MyIssueService {

	MyIssueDao dao = new MyIssueDao();

	public List<Article> getMyIssueArticleList(String uid) {
		List<Article> articleList = null;
		try {
			articleList = dao.getArticleOrderList(uid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return articleList;
	}

	public Map<String, Object> showMyIssueArticleDetail(String aid, String foundFlag, String invalidFlag) {
		Map<String, Object> article = null;
		try {
			article = dao.showMyIssueArticleDetail(aid,foundFlag,invalidFlag);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return article;
	}

	public boolean confirmOrderArticle(FoundArticle foundArticle) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleAddFoundArticle(foundArticle);
			dao.handleUpdateFoundFlag(foundArticle);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}

	public boolean handleDeleteArticleByAid(String aid) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleDeleteArticleInFoundArticle(aid);
			dao.handleDeleteArticleInArticle(aid);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
}
